package com.duke.shaking;

import java.util.Date;
import java.util.Random;

import com.duke.shaking.auth.LoginActivity;
import com.duke.shaking.pojo.User;
import com.duke.shaking.register.Register1Activity;
import com.duke.shaking.utils.PrefUtils;

import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

public class MainActivity extends BaseActivity {

	public static final int initialMembersNum = 212345;
	
	private TextView txtMembers;
	private Thread membersChangeThread;
	private Random rand;
	private boolean stoped;
	private Handler handler;
	private int members;
	private PrefUtils confPrefUtils;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        
        confPrefUtils = PrefUtils.getInstance(this, this.getString(R.string.configPref));
        members = confPrefUtils.getInt(getString(R.string.conf_fakeMembers), initialMembersNum);
        
        txtMembers = (TextView)findViewById(R.id.txt_members);
        handler = new Handler();
        startMembersThread();
    }

    private class MembersRunnable implements Runnable {
    	private int members;
    	public MembersRunnable(int members) {
    		this.members = members;
    	}
    	
		public void run() {
			if (txtMembers != null) {
				confPrefUtils.addInt(getString(R.string.conf_fakeMembers), members);
				txtMembers.setText(String.valueOf(members));
			}
			
		}
    	
    }
    
    private void startMembersThread() {
    	rand = new Random();
    	rand.setSeed((new Date()).getTime());
    	
    	membersChangeThread = new Thread() {
            @Override
            public void run() {
            	try {
            		while(!stoped) {
            			int delta = rand.nextInt(50);
            			if (delta <= 0) {
            				delta = 1;
            			}
            			members += delta;
            			handler.post(new MembersRunnable(members));
            			sleep(1 * 1000);
            		}
            	}
            	catch (InterruptedException e) {
            		
            	}
                finally {
                    
                    //stop();
                }
            }
        };
        membersChangeThread.start();
	}
    
    @Override
    protected void onPause() {
    	stoped = true;
    	super.onPause();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void girlsRegister(View v) {
    	User regUser = new User();
    	regUser.setSex(User.sex_female);
    	go(Register1Activity.class, "regUser", regUser);
    }
    
    public void boysRegister(View v) {
    	User regUser = new User();
    	regUser.setSex(User.sex_male);
    	go(Register1Activity.class, "regUser", regUser);
    }
    
    public void login(View v) {
    	finish();
    	go(LoginActivity.class, null, null);
    }
}
